﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HZU.ZP.Domain.IService;
using HZU.ZP.Domain.IService.Resume;
using HZU.ZP.Domain.Model;
using HZU.ZP.Help;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace HZU.ZP.Controllers
{
    public class MessageController : Controller
    {
        private readonly IHubContext<MyHub> _myHub;
        private IMessageService _messageService;
        private ICompanyService _companyService;
        private IApplicantService _applicantService;
        private IInformationService _informationService;

        public MessageController(IHubContext<MyHub> myHub, IMessageService messageService,
            ICompanyService companyService, IApplicantService applicantService,
            IInformationService informationService)
        {
            _myHub = myHub;
            _messageService = messageService;
            _companyService = companyService;
            _applicantService = applicantService;
            _informationService = informationService;
        }
        // GET: /<controller>/
        public IActionResult Index()
        {
            return View();
        }

        [Authorize]
        [HttpGet]
        public IActionResult AllMsg(string email, string toEmail = "")
        {
            Company company = null;
            Message message = new Message();
            IList<Message> messages = new List<Message>();            
            switch (User.Claims.ToList()[2].Value)
            {
                case "Company":
                    ViewData["FromName"] = _companyService.GetByEmail(email).CompanyName;
                    break;
                case "Applicant":
                    var app = _applicantService.GetApplicant(email);
                    if (app != null)
                    {
                        var information = _informationService.GetInformation(app.Id.Value);
                        if (information != null)
                            ViewData["FromName"] = information.Name;
                        else
                            ViewData["FromName"] = email;
                    }
                    if (!string.IsNullOrEmpty(toEmail))
                        company = _companyService.GetByEmail(toEmail);
                    break;
            }
            messages = _messageService.GetMesUserList(email, email + User.Claims.ToList()[2].Value);
            if (company != null)
            {
                message.FromEmail = email;
                message.ToEmail = company.Email;
                message.ToName = company.CompanyName;
                message.ReadState = 1;
                for (int i = messages.Count - 1; i >= 0; i--)
                {
                    if (messages[i].FromEmail.Equals(company.Email) || messages[i].ToEmail.Equals(company.Email))
                        messages.Remove(messages[i]);
                }
                messages.Insert(0, message);
            }

            ViewData["Title"] = "消息";
            return View(messages);
        }

        [Authorize]
        [HttpPost]
        public async Task<IActionResult> PostData([FromBody]Message message)
        {            
            message.CreateTime = DateTime.Now;
            message.ReadState = 0;
            _messageService.Add(message);
            if (!string.IsNullOrWhiteSpace(message.ToId))
                await _myHub.Clients.User(message.ToId).SendAsync("ReceiveMessage", message);
            return Json(AjaxResult.ReturnMessage(1, "发送成功"));
        }

        [Authorize]
        [HttpPost]
        public IActionResult Update(int id)
        {
            Message message = new Message();           
            message = _messageService.GetById(id);
            message.ReadState = 1;
            _messageService.Update(message);
            return Json(AjaxResult.ReturnMessage(1, "阅读成功"));
        }

        [Authorize]
        [HttpPost]
        public IActionResult UpdateList(string fromEmail, string toEmail)
        {
            string sql = $"update messages set readState = 1 where fromEmail = '{fromEmail}' and toEmail = '{toEmail}' and readState=0";
            _messageService.UpdateList(sql);
            return Json(AjaxResult.ReturnMessage(1, "阅读成功"));
        }

        [Authorize]
        [HttpGet]
        public IActionResult GetMes(string fromEmail, string toEmail)
        {
            IList<Message> messages = new List<Message>();            
            messages = _messageService.GetMesList(fromEmail, toEmail);
            return Json(AjaxResult.ReturnMessage(1, "操作成功", messages));
        }
    }
}
